<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2020-09-03 09:57:40
 * @LastEditors: 灰佳小白 1150248441@qq.com
 * @LastEditTime: 2022-09-19 19:33:25
-->
<template>
  <div>
    <div style="float: left; display: flex">
      <el-input
        placeholder="请输入新闻名"
        v-model="newsName"
        clearable
        style="margin-right: 10px; width: 300px"
        @clear="clear"
      >
      </el-input>
      <el-select
        v-model="newsCate"
        placeholder="请选择新闻分类"
        clearable
        style="margin-right: 10px; width: 300px"
        @clear="clear"
      >
        <el-option
          v-for="item in cateData"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-button type="primary" @click="searchNews" style="margin-bottom: 10px">
        <i class="iconfont el-icon-search" /> 搜索</el-button
      >
    </div>
    <div style="float: right">
      <el-button type="success" @click="toAddNews">
        <i class="iconfont el-icon-circle-plus-outline" />新增</el-button
      >
    </div>
    <div class="table" style="margin-bottom: 20px">
      <el-table :data="newsData" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="title" label="新闻标题" align="center">
        </el-table-column>
        <el-table-column
          prop="category.name"
          label="新闻分类"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column label="发布时间" width="250" align="center">
          <template slot-scope="scope">
            {{ scope.row.publishTime | fmtDate }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              @click="toEditNews(scope.row)"
              type="primary"
              size="small"
            >
              <i class="iconfont el-icon-edit" /> 修改</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="toDeleteNews(scope.row)"
              ><i class="iconfont el-icon-delete"></i>删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      background
    >
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model.trim="form.title"
            placeholder="请输入新闻标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择所属分类">
            <el-option
              v-for="item in cateData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 富文本编辑器 -->
        <el-form-item label="资讯">
          <Tinymce v-model="form.content" :height="200" />
          <!-- <el-input type="textarea" v-model="form.description"></el-input> -->
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="updateNews">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
//引入富文本编辑器
import Tinymce from "@/components/Tinymce";
export default {
  //注册组件
  components: {
    Tinymce,
  },
  data() {
    return {
      page: 1,
      dialogVisible: false,
      cateData: [],
      newsName: "",
      form: {},
      title: "",
      pageSize: 10,
      total: 0,
      newsData: [],
      newsCate: "",
      rules: {
        //新闻标题的表单验证
        title: [
          {
            required: true,
            message: "请输入新闻标题",
            trigger: "blur",
          },
        ],
        categoryId: [
          {
            required: true,
            message: "请选择所属分类",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    //关闭模态框的回调
    handleClose() {
      this.dialogVisible = false;
      this.form = {};
    },
    //模态框确认的回调
    async updateNews() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          //将表单对象提交到对应接口中
          let res = await post("/article/saveOrUpdate", this.form);
          // console.log(res);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            this.form = {};
            this.getNewsData();
          } else {
            this.$notify({
              title: "成功",
              message: "这是一条成功的提示消息",
              type: "error",
            });
          }
          this.dialogVisible = false;

          this.getNewsData();
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    //添加新闻
    toAddNews() {
      this.title = "发布新闻资讯";
      this.form = {};
      this.dialogVisible = true;
    },

    // 编辑新闻
    toEditNews(row) {
      this.title = "编辑新闻资讯";
      this.dialogVisible = true;
      this.form = {
        ...row,
      };
    },
    //删除新闻
    toDeleteNews(row) {
      this.$confirm("此操作将永久删除该新闻, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await get("/article/deleteById", {
            id: row.id,
          });
          // console.log(res);
          if (res.status == 200) {
            this.$message.success(res.message + " !");
            this.getNewsData();
          } else {
            this.$message.error("删除失败！");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.searchNewsPage();
    },
    //获取所有分类信息
    async getCategory() {
      let res = await get("/category/findAll");
      // console.log(res.data);
      this.cateData = res.data;
    },
    //获取所有新闻信息
    async getNewsData() {
      let params = {
        page: this.page,
        pageSize: this.pageSize,
      };
      let res = await get("/article/pageQuery", params);
      // console.log(res.data.list);
      this.newsData = res.data.list;
      this.total = res.data.total;
    },
    //搜索
    async searchNews() {
      console.log(this.newsCate);
      let params = {
        page: 1,
        pageSize: this.pageSize,
        title: this.newsName,
        categoryId: this.newsCate,
      };
      let res = await get("/article/pageQuery", params);
      // console.log(res.data.list);
      this.newsData = res.data.list;
      this.total = res.data.total;
    },
    // 搜索时切换分页
    async searchNewsPage() {
      let params = {
        page: this.page,
        pageSize: this.pageSize,
        title: this.newsName,
        categoryId: this.newsCate,
      };
      let res = await get("/article/pageQuery", params);
      // console.log(res.data.list);
      this.newsData = res.data.list;
      this.total = res.data.total;
    },
    clear() {
      this.searchNews();
    },
  },
  created() {
    this.getNewsData();
    this.getCategory();
  },
};
</script>

<style scoped>
</style>
